﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AsyncCheapPool
{
    public class ItemToken<T> : IDisposable
    {
        internal ItemToken(T value, Action dispose)
        {
            _value = value;
            _dispose = dispose;
        }

        Action _dispose;

        T _value;

        public T Value
        {
            get { return _value; }

        }



        bool _isDisposed;

        public void Dispose(bool isFinallizing)
        {
            if (!_isDisposed)
            {
                try
                {
                    _dispose();
                }
                catch (Exception)
                {


                }
                finally
                {
                    _isDisposed = true;
                }
            }
            if (!isFinallizing)
            {
                GC.SuppressFinalize(this);
            }

        }
        public void Dispose()
        {

            Dispose(false);


        }
        ~ItemToken()
        {
            Dispose(true);
        }
    }
}
